<template>
  <div class="container">
    <!--添加学院对话框表单-->
    <el-form :inline="true" :model="listQuery" class="demo-form-inline">
      <el-form-item>
    <el-button type="primary" @click="addClass">添加班级</el-button>
      </el-form-item>
      <el-form-item label="学院">
        <el-select v-model="listQuery.collegeId" :clearable="1===1" placeholder="请选择学院">
          <el-option
            v-for="item in collegeOptions"
            :key="item.id"
            :label="item.collegeName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="添加班级" :visible.sync="dialogFormVisible"  @close="onDialogClose">
      <el-form :model="listQuery" ref="form" :rules="rules">
        <el-form-item label="学院名称" :label-width="formLabelWidth"  prop="collegeId">
          <el-select v-model="listQuery.collegeId" placeholder="请选择学院">
            <el-option
              v-for="item in collegeOptions"
              :key="item.id"
              :label="item.collegeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级名称" :label-width="formLabelWidth" prop="className">
          <el-input v-model="listQuery.className" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditClass('form')">确 定</el-button>
      </div>
    </el-dialog>
    <!--学院列表表格-->
    <el-table
      :data="classData"
      border
      v-loading="loading"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
      >
      </el-table-column>
      <el-table-column
        prop="className"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="handleEditClick(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDeleteClick(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[1, 10, 15]"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {addClass, deleteClass, getClassList} from "../../api/class";
  import {getCollegedropDownList} from "../../api/common";
  import {addCollege} from "../../api/college";

  export default {
    name: "",
    data() {
      return {
        loading:false,
        classData: [],
        dialogFormVisible:false,
        formLabelWidth: '120px',
        collegeOptions:[],
        total:0,
        listQuery: {
          page: 1,
          size: 10,
          className:'',
          typeForEdit:'',
          collegeId:'',
          id:''
        },
        rules: {
          className: [
            { required: true, message: '请输入班级名称', trigger: 'blur' }
          ],
          collegeId:[
            { required: true, message: '请选择学院', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      //关闭添加对话框
      onDialogClose(){
        this.listQuery.className=''
        this.listQuery.collegeId=''
      },
      listClass() {
        this.loading=true
        getClassList(this.listQuery).then(res => {
          this.classData = res.data.pageList
          this.total=res.data.total
          this.loading=false
        }).catch(err=>[
          this.loading=false
        ])
      },
      addOrEditClass(formName){
        this.loading=true
        this.$refs[formName].validate(valid=>{
          if (valid) {
            this.dialogFormVisible = false;
            console.log(this.listQuery.collegeId);
            addClass(this.listQuery).then(res => {
              this.$message.success("添加成功")
              this.listClass()
            }).catch(err => {
              this.loading=false
            });
          } else {
            this.$message.warning("请完善信息")
          }
        })

      },
      //获取学院列表
      getdropDownListForCollege() {
        getCollegedropDownList(1484744242220589057)
          .then(res => {
            console.log(res)
            this.collegeOptions = res.data
          })
      },
      //添加学院
      addClass(){
        this.dialogFormVisible=true
        this.getdropDownListForCollege()
        this.listQuery.typeForEdit="add"

      },
      //修改学院信息
      handleEditClick(param){
        console.log(param)
        this.getdropDownListForCollege()
        this.dialogFormVisible=true
        this.listQuery.collegeId=param.collegeId
        this.listQuery.className=param.className
        this.listQuery.TypeForEdit="edit"
      },
      //删除班级
      handleDeleteClick(param){
        this.loading=true
        this.listQuery.id=param.id
        deleteClass(this.listQuery).then(res=>{
          this.$message.success("删除成功")
          this.listClass()
        }).catch(error=>{
          this.$message.error("删除失败")
          this.loading=false
        })
      },
      //分页相关
      handleSizeChange(val) {
        this.listQuery.size=val
        this.getStudentList()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.listQuery.page=val
        this.getStudentList()
        console.log(`当前页: ${val}`);
      },
      //点击查询
      onSubmit() {
        this.listClass()
      },
    },
    created() {
      this.getdropDownListForCollege()
      this.listClass()
    }
  }
</script>

<style scoped>
  .demo-form-inline{
    margin-top: 20px;
    margin-left: 10px;
  }
</style>
